package com.zjp.annotation;

import java.lang.reflect.Method;

/**
 * @author: zjp
 * @date: 2023-12-02
 * @description: 测试自定义注解
 */
@MyAnotation(age = 18, isMan = true, hobby = {"刷抖音"})
public class Test6_MyAnotation {
    public static void main(String[] args) throws NoSuchMethodException {
        // 拿到类的信息
        Class<Test6_MyAnotation> clazz = Test6_MyAnotation.class;

        // 判断 MyAnotation 注解是否在当前类上
        if (clazz.isAnnotationPresent(MyAnotation.class)){
            // 拿到类上的这个注解对象
            MyAnotation myAnotation = clazz.getDeclaredAnnotation(MyAnotation.class);
            System.out.println("类上的注解的信息：" + myAnotation.name() + "--" + myAnotation.age());
        }

        // 获取该类的 test() 方法
        Method testMethod = clazz.getDeclaredMethod("test");
        // 判断 MyAnotation 注解是否在该方法上
        if (testMethod.isAnnotationPresent(MyAnotation.class)){
            // 拿到方法上的这个注解对象
            MyAnotation myAnotation = testMethod.getDeclaredAnnotation(MyAnotation.class);
            System.out.println("方法上的注解的信息：" + myAnotation.name() + "--" + myAnotation.age());
        }
    }

    @MyAnotation(name = "lxh", age = 20, isMan = true, hobby = "打游戏")
    public static void test(){

    }
}
